/*
* $Id: HousingDialog.cpp 40 2010-02-25 12:59:54Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "HousingDialog.h"
#include "Database/Tables/HousingsTable.h"

CHousingDialog::CHousingDialog()
: CDialog(IDD_NEW_HOUSE, dynamic_cast<CWnd*>(IApplication::GetApp()->GetVisualManager()->
		  GetVisualComponent(IApplication::GetApp()->GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame())),
	m_nFrames(0), m_nIdHousing(0), m_nIdFamily(0), m_nTypeHousing(0), m_bUpdate(false){}
CHousingDialog::CHousingDialog(int nId, std::tstring strName, int nType, int nFrames, int nIdFamily)
: CDialog(IDD_NEW_HOUSE, dynamic_cast<CWnd*>(IApplication::GetApp()->GetVisualManager()->
		  GetVisualComponent(IApplication::GetApp()->GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame())),
	m_nIdHousing(nId), m_strName(strName), m_nTypeHousing(nType), m_nFrames(nFrames), m_nIdFamily(nIdFamily), m_bUpdate(true){}
CHousingDialog::~CHousingDialog(){}

BOOL CHousingDialog::OnInitDialog(){
	BOOL bRes = __super::OnInitDialog();
	SetWindowText(IApplication::GetApp()->GetLanguageManager()->GetString(ADD_HOUSING_ID).c_str());
	SetDlgItemText(IDC_NAME_HOUSE, IApplication::GetApp()->GetLanguageManager()->GetString(HOUSING_NAME_ID).c_str());
	SetDlgItemText(IDC_TYPE_HOUSE, IApplication::GetApp()->GetLanguageManager()->GetString(HOUSING_TYPE_ID).c_str());
	SetDlgItemText(IDC_TYPE_FRAME, IApplication::GetApp()->GetLanguageManager()->GetString(HOUSING_FRAME_ID).c_str());
	if(m_bUpdate)
		SetDlgItemText(IDOK, IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_ID).c_str());
	else
		SetDlgItemText(IDOK, IApplication::GetApp()->GetLanguageManager()->GetString(ADD_ID).c_str());
	SetDlgItemText(IDCANCEL, IApplication::GetApp()->GetLanguageManager()->GetString(CANCEL_ID).c_str());

	SetDlgItemText(IDC_NAMEH_EDIT, m_strName.c_str());

	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(EIGHT_FRAME_HOUSING_ID).c_str());
	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(TEN_FRAME_HOUSING_ID).c_str());
	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(TWELVE_FRAME_HOUSING_ID).c_str());
	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(SIXTEEN_FRAME_HOUSING_ID).c_str());
	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(TWENTY_FRAME_HOUSING_ID).c_str());
	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(SECTION_HOUSING_ID).c_str());
	m_typeHousing.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(ANOTHER_FRAME_HOUSING_ID).c_str());
	m_typeHousing.SetCurSel(m_nTypeHousing);

	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(S300_ID).c_str());
	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(S230_ID).c_str());
	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(S145_ID).c_str());
	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(SECTION_ID).c_str());
	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(W230_ID).c_str());
	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(W145_ID).c_str());
	m_typeFrame.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(ANOTHER_FRAME_ID).c_str());
	m_typeFrame.SetCurSel(m_nFrames);
	return bRes;
}

void CHousingDialog::DoDataExchange(CDataExchange* pDX){
	DDX_Control(pDX, IDC_TYPEH_EDIT, m_typeHousing);
	DDX_Control(pDX, IDC_TYPEF_EDIT, m_typeFrame);
	__super::DoDataExchange(pDX);
}

void CHousingDialog::OnOK(){
	CString strValue;
	GetDlgItemText(IDC_NAMEH_EDIT, strValue);
	m_strName = strValue.GetString();
	m_nTypeHousing = m_typeHousing.GetCurSel();
	m_nFrames = m_typeFrame.GetCurSel();
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable table;
	table.m_content.s_strName = m_strName;
	table.m_content.s_nType = m_nTypeHousing;
	table.m_content.s_nFrames = m_nFrames;
	table.m_content.s_nIdFamily = m_nIdFamily;
	if(m_bUpdate){
		table.m_content.s_bDelete = false;
		table.m_content.s_nId = m_nIdHousing;
		table.UpdateValue(pDb);
	}else{
		table.SetValue(pDb);
		m_nIdHousing = table.m_content.s_nId;
	}
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	EndDialog(IDOK);
}

int CHousingDialog::GetIdHousing(){
	return m_nIdHousing;
}

std::tstring CHousingDialog::GetNameHousing(){
	return m_strName;
}

int CHousingDialog::GetTypeHousing(){
	return m_nTypeHousing;
}

int CHousingDialog::GetTypeFrames(){
	return m_nFrames;
}